#include "DLinkList.h"


#define   LIST_TEST_SIZE                (10)

int CmpFunc (const void* _data,const void* _param)
{
    if ( (int*)(_data) > ((int*)(_param) + 2)  )  /*found data should be less (-2) than is list*/
    {
        return 1;
    }
    return 0;
}

#ifdef TEST_DLINK_LIST
int main(void)
{
    /*let make a list of ints*/
    int i,count,ints[LIST_TEST_SIZE];
    List* list;
    for (i=0 ; i<LIST_TEST_SIZE; ++i)
    {
        ints[i] = i;
    }
    list =   ListNew();
    printf("is list  empty?: %d\n",count = ListIsEmpty(list));

    for (i=0 ; i<LIST_TEST_SIZE/2; ++i)
    {
        ListPushH(list,(void*)(ints + i));
        if (1 == ListIsEmpty(list))
        {
            printf("ListIsEmpty Failed\n");
        }
        printf("on %d head push : count = %ld new head is %d\n",i, (ListCount(list)), *(int*)(ListHead(list)));
    }
    for (i=0 ; i< LIST_TEST_SIZE/2; ++i)
    {
        if (1 == ListIsEmpty(list))
        {
            printf("ListIsEmpty Failed\n");
        }
        printf("before  %d head pop : count = %ld new head is %d\n",i, (ListCount(list)), *(int*)(ListHead(list)));
        ListPopH(list);
    }

    for (i=0 ; i<LIST_TEST_SIZE/2; ++i)
    {
        ListPushT(list,(void*)(ints + i));
        if (1 == ListIsEmpty(list))
        {
            printf("ListIsEmpty Failed\n");
        }
        printf("on %d tail push : count = %ld new tail is %d\n",i, ListCount(list), *(int*)(ListTail(list)));
    }

    for (i=0 ; i< LIST_TEST_SIZE/2; ++i)
    {
        if (1 == ListIsEmpty(list))
        {
            printf("ListIsEmpty Failed\n");
        }
        printf("before  %d head pop : count = %ld new head is %d\n",i, ListCount(list), *(int*)(ListTail(list)));
        ListPopT(list);
    }
    if (1 != ListIsEmpty(list))
    {
        printf("ListIsEmpty Failed\n");
    }
    printf("multiple Head Pushes:\n");
    count = ListPushHM(list,sizeof(int),10, ints);
    if ( 0 != count )
    {
        printf("multiple Head Pushes failed\n");
    }
    for (i=0 ; i< 10 ; ++i)
    {
        printf("before  %d head pop : count = %ld new head is %d\n",i, ListCount(list), *(int*)(ListHead(list)));
        ListPopH(list);
    }

    printf("multiple Tail Pushes:\n");
    count = ListPushTM(list,sizeof(int),10, ints);
    if ( 0 != count )
    {
        printf("multiple Tail Pushes failed\n");
    }
    for (i=0 ; i< 10 ; ++i)
    {
        printf("before  %d tail pop : count = %ld new head is %d\n",i, ListCount(list), *(int*)(ListTail(list)));
        ListPopT(list);
    }

    printf("multiple Tail Pushes:\n");
    count = ListPushTM(list,sizeof(int),10, ints);
    count = *(int*)(ListFindIf((const List*)list,CmpFunc,(const void*)(ints)));
    printf("looking for first element larger than 2 in list (should be 3) , results =    %d\n", count);

    ListDel(list);
    printf("all is well!\n");
    return 0 ;
}
#endif   /*TEST_DLINK_LIST*/


